<?php
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $username = !empty($match[2]) ? $match[2] : "";
        
        $user = getUserByUsername($username);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;        
        $result["username"] = $username;        
        
        return $result;
    }
    
    function getUserByUsername($username)
    {
        $sql = "SELECT id, username FROM c_user WHERE `username`='" . mysql_escape_string($username) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;
    } 
           
    /*
    * processing
    */
    
    function processing($data)
    {        
        $regx = "/^TEST(\s+)([a-zA-Z0-9_]{1,})$/si";                
        
        if(preg_match($regx, $data["content"]))
        {            
            $list_service_code = array("6022");
            $syntax = buildSyntax($data["content"], $regx);
            
            $data["username"] = $syntax["username"];
            $data["userId"] = $syntax["userId"];
            
            if(!empty($data["userId"])){
                if(in_array($data["serviceCode"], $list_service_code)){
                    
                    $response["status"] = 1;                
                    $response["msg"] = "Ban da gui tin nhan thanh cong. Chuc ban kinh doanh tot tren taoviec";
                
                    $data["status"] = 1; 
                                        
                } else {
                    $response["status"] = 3;                        
                    $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                                        
                    $data["status"] = 0;                 
                }
                   
            } else {
                $response["status"] = 2;                
                $response["msg"] = "Tin nhan sai cu phap";    
                                
                $data["status"] = 0;
            }
                               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
                        
            $data["userId"] = 0;                
            $data["status"] = 0;      
        }
        
        /* log sms */
        $data["transactionId"] = $data["transactionId"];
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 1;
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];            
        $data["id"] = 0;            
        $data["type"] = 7;                    
        
        $result = InsertSmsLogNoPercent($data);   
        return $response;
    }
?>